package com.zretc.point.controller;

import com.zretc.api.pojo.Point;
import com.zretc.api.pojo.User;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.point.service.PointService;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/point")
public class PointController {
    @Autowired
    private PointService pointService;


    @GetMapping("/actions")
    public RespResult getPointList(){
        System.out.println("PointController================================================="); // test
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        System.out.println(user); // test
        return R.success(pointService.queryPointByUserId(user.getUser()));
    }

    @PostMapping("/clockIn")
    public RespResult clockIn(){
        System.out.println("PointController--------------------------"); // test

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();

        //System.out.println(point.getUserId()); // test

        return pointService.getPointByClockIn(user.getUser().getUserId());
    }

    @GetMapping("/total")
    public RespResult getTotalPoint(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        return pointService.getTotalPoint(user.getUser().getUserId());
    }

    @GetMapping("/reputation")
    public RespResult getReputation(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        return pointService.getReputation(user.getUser().getUserId());
    }

    // PointFeignClient
    @GetMapping("/addBlog")
    public RespResult addBlog(@RequestParam("userId") Integer userId){
        System.out.println("PointController--------------------------"); // test
        System.out.println(userId); // test
        //System.out.println(username); // test
        return pointService.getPointByAddBlog(userId);
    }
}
